package everyday;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Siantar
 * @Date: 2023-06-03-14:56
 * @Description: 1.0
 */
public class T1156 {
    public int maxRepOpt1(String text) {
        Map<Character, Integer> count = new HashMap<>();
        int len = text.length();
        for (char c : text.toCharArray()) {
            count.put(c, count.getOrDefault(c, 0) + 1);
        }
        int res = 0;
        for (int i = 0; i < len; ) {
            int j = i;
            while (j < len && text.charAt(j) == text.charAt(i)) {
                j++;
            }
            if (j - i < count.get(text.charAt(i)) && (j < len || i > 0)) {
                res = Math.max(res, j - i);
            }
            int k = j + 1;
            while (k < len && text.charAt(k) == text.charAt(i)) {
                k++;
            }
            res = Math.max(res, Math.min(k - i, count.get(text.charAt(i))));
            i = j;
        }
        return res;
    }
}
